/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified on 02/16/2008
 */
package tn.talan.poi;
 
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 
public class ReadExcelFile {
 
    public static void main( String [] args ) {
 
        String fileName="test.xls";
        //Read an Excel File and Store in a Vector
        List dataHolder=readExcelFile(fileName);
        //Print the data read
        printCellDataToConsole(dataHolder);
    }
    public static List readExcelFile(String fileName)
    {
        /** --Define a Vector
            --Holds Vectors Of Cells
         */
        List liste=new ArrayList<String>();
 
        try{
        /** Creating Input Stream**/
        //InputStream myInput= ReadExcelFile.class.getResourceAsStream( fileName );
        FileInputStream myInput = new FileInputStream(fileName);
 
        /** Create a POIFSFileSystem object**/
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
 
        /** Create a workbook using the File System**/
         HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
 
         /** Get the first sheet from workbook**/
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
 
        /** We now need something to iterate through the cells.**/
          Iterator rowIter = mySheet.rowIterator();
 
          while(rowIter.hasNext()){
              HSSFRow myRow = (HSSFRow) rowIter.next();
              Iterator cellIter = myRow.cellIterator();
              List cellStoreVector=new  ArrayList<String>();
              while(cellIter.hasNext()){
                  HSSFCell myCell = (HSSFCell) cellIter.next();
                  cellStoreVector.add(myCell);
              }
              liste.add(cellStoreVector);
          }
        }catch (Exception e){e.printStackTrace(); }
        return liste;
    }
 
    private static void printCellDataToConsole(List liste) {
 
        for(int k=0;k<liste.size();k++){
                HSSFCell myCell = (HSSFCell)liste.get(k);
                String stringCellValue = myCell.toString();
                System.out.println(stringCellValue+"\n");
            }
            System.out.println();
        }
    }
